import { getAllRoleList, getAllPositionList } from '@/views/children/auth/js/auth'
// import { getUserScene } from '@/api/user'

// import base from '@/views/children/auth/js/index'
// import { constantRouterMap } from '@/router/baseRouter'

const auth = {
  state: {
    // routers: constantRouterMap,
    addRouters: [],
    permissionList: [], // 权限
    roleList: [], // 角色
    positionList: [] // 岗位
  },

  mutations: {
    setRouters: (state, data) => {
      state.addRouters = data
      // state.routers = constantRouterMap.concat(data)
    },
    setPermissionList: (state, permissionList) => {
      state.permissionList = permissionList
    },
    setRoleList (state, roleList) {
      state.roleList = roleList
    },
    setPositionList (state, positionList) {
      state.positionList = positionList
    }
  },

  actions: {
    // // 动态添加主界面路由，需要缓存
    // UpdateAppRouter ({ commit }, routes) {
    //   return new Promise(resolve => {
    //     let routelist = routes.constRoutes
    //     commit('setRouters', routelist)
    //     resolve()
    //   })
    // },
    // // 获取用户信息
    // GetPermissionList ({ commit }) {
    //   return new Promise((resolve, reject) => {
    //     getUserScene()
    //       .then(res => {
    //         let useInfo = {
    //           userNo: res.data.userNo,
    //           userName: res.data.userName,
    //           remark: res.data.remark,
    //           mobile: res.data.mobile,
    //           tenantNo: res.data.tenantNo,
    //           userType: res.data.userType
    //         }
    //         base().setLocalStorage('userinfo', useInfo)
    //         let data = res.data.sceneTreeResps.filter(e => e.sceneNo === localStorage.getItem('sceneNo'))
    //         if (data && data.length === 0) return
    //         let newMenu = base().generateIndexRouter(data)
    //         if (newMenu && newMenu.length > 0) {
    //           commit('setPermissionList', newMenu)
    //         }
    //         resolve(newMenu)
    //       })
    //       .catch(err => reject(err))
    //   })
    // },

    // 所有角色
    getAllRoleList ({ commit }) {
      return new Promise((resolve, reject) => {
        getAllRoleList()
          .then(res => {
            if (res.code === 0) {
              commit('setRoleList', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 所有岗位
    getAllPositionList ({ commit }) {
      return new Promise((resolve, reject) => {
        getAllPositionList()
          .then(res => {
            if (res.code === 0) {
              commit('setPositionList', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default auth
